#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include "../include/db.h"

#define DB_FILE "sfts.db"

// 初始化数据库
int init_database(sqlite3 **ppDb)
{
    int rc = sqlite3_open(DB_FILE, ppDb);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(*ppDb));
        *ppDb = NULL; // 确保在失败时指针被设为NULL
        return -1;
    }

    char *err_msg = NULL;

    // Users table (table name 'users', column names lowercase for consistency)
    const char *sql_create_users_table =
        "CREATE TABLE IF NOT EXISTS users ("
        "id INTEGER PRIMARY KEY AUTOINCREMENT,"
        "username TEXT UNIQUE NOT NULL,"
        "password_hash BLOB NOT NULL,"
        "salt BLOB NOT NULL," /* Added salt column */
        "permission_level INTEGER DEFAULT 0,"
        "created_at DATETIME DEFAULT CURRENT_TIMESTAMP,"
        "last_login DATETIME"
        ");";
    rc = sqlite3_exec(*ppDb, sql_create_users_table, 0, 0, &err_msg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "创建 'users' 表失败: %s\n", err_msg);
        sqlite3_free(err_msg);
        sqlite3_close(*ppDb);
        *ppDb = NULL;
        return -1;
    }
    fprintf(stdout, "表 'users' 初始化/检查成功.\n");

    // Audit Log table (table name 'audit_log', column names lowercase)
    const char *sql_create_audit_log_table =
        "CREATE TABLE IF NOT EXISTS audit_log ("
        "id INTEGER PRIMARY KEY AUTOINCREMENT,"
        "timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,"
        "user_id INTEGER,"
        "username TEXT,"
        "operation TEXT NOT NULL,"
        "file_path TEXT,"
        "status INTEGER," /* 0: success, 1: failure */
        "ip_address TEXT,"
        "previous_log_hash BLOB UNIQUE,"
        "current_log_hash BLOB UNIQUE NOT NULL,"
        "FOREIGN KEY (user_id) REFERENCES users(id)"
        ");";
    rc = sqlite3_exec(*ppDb, sql_create_audit_log_table, 0, 0, &err_msg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "创建 'audit_log' 表失败: %s\n", err_msg);
        sqlite3_free(err_msg);
        sqlite3_close(*ppDb);
        *ppDb = NULL;
        return -1;
    }
    fprintf(stdout, "表 'audit_log' 初始化/检查成功.\n");

    // File Permissions table (table name 'file_permissions', column names lowercase)
    const char *sql_create_file_permissions_table =
        "CREATE TABLE IF NOT EXISTS file_permissions ("
        "id INTEGER PRIMARY KEY AUTOINCREMENT,"
        "user_id INTEGER NOT NULL,"
        "file_path TEXT NOT NULL,"
        "can_read INTEGER DEFAULT 0,"
        "can_write INTEGER DEFAULT 0,"
        "can_delete INTEGER DEFAULT 0,"
        "UNIQUE (user_id, file_path),"
        "FOREIGN KEY (user_id) REFERENCES users(id)"
        ");";
    rc = sqlite3_exec(*ppDb, sql_create_file_permissions_table, 0, 0, &err_msg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "创建 'file_permissions' 表失败: %s\n", err_msg);
        sqlite3_free(err_msg);
        sqlite3_close(*ppDb);
        *ppDb = NULL;
        return -1;
    }
    fprintf(stdout, "表 'file_permissions' 初始化/检查成功.\n");

    // init_permissions_table(*ppDb); // 调用权限初始化，如果需要

    printf("数据库初始化成功\n");
    return 0;
}

// 关闭数据库连接
void close_database(sqlite3 *db)
{
    if (db)
    {
        sqlite3_close(db);
    }
}